<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑合同 - HRM系统</title>
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <link rel="stylesheet" th:href="@{/css/contract-edit.css}">
</head>
<body>
<div class="dashboard">
    <header class="header">
        <div class="logo">HRM系统</div>
        <div class="user-info">
            <span th:text="${currentUser.realName}"></span>
            <span class="role-badge" th:text="${currentUser.roleId.roleName}"></span>
            <a href="/logout" class="logout-btn">退出</a>
        </div>
    </header>

    <main class="contract-edit-container">
        <div class="back-container">
            <a th:href="@{'/contract/view/' + ${contract.contractId}}" class="btn btn-back">← 返回合同详情</a>
            <a th:href="@{/contract}" class="btn btn-back">← 返回合同列表</a>
        </div>

        <div class="module-header">
            <h1 class="module-title">编辑合同</h1>
        </div>

        <form th:action="@{'/contract/edit/' + ${contract.contractId}}" method="post" class="contract-form">
            <input type="hidden" th:name="contractId" th:value="${contract.contractId}">

            <div class="form-section">
                <h3>基本信息</h3>
                <div class="form-row">
                    <div class="form-group">
                        <label for="contractNumber">合同编号</label>
                        <input type="text" id="contractNumber" th:field="*{contract.contractNumber}" readonly>
                    </div>
                    <div class="form-group">
                        <label for="contractName">合同名称</label>
                        <input type="text" id="contractName" th:field="*{contract.contractName}" required>
                    </div>
                    <div class="form-group">
                        <label for="contractType">合同类型</label>
                        <select id="contractType" th:field="*{contract.contractType}" required>
                            <option th:each="type : ${contractTypes}"
                                    th:value="${type}"
                                    th:text="${type}"
                                    th:selected="${type == contract.contractType}">
                            </option>
                        </select>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="effectiveDate">生效日期</label>
                        <input type="date" id="effectiveDate" th:field="*{contract.effectiveDate}" required>
                    </div>
                    <div class="form-group">
                        <label for="expiryDate">到期日期</label>
                        <input type="date" id="expiryDate" th:field="*{contract.expiryDate}" required>
                    </div>
                    <div class="form-group">
                        <label for="isTransferable">是否可转让</label>
                        <select id="isTransferable" th:field="*{contract.isTransferable}">
                            <option value="true">是</option>
                            <option value="false">否</option>
                        </select>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="penaltyRate">违约金利率</label>
                        <input type="number" id="penaltyRate" th:field="*{contract.penaltyRate}" step="0.001" min="0">
                    </div>
                </div>
            </div>

            <!-- 动态内容部分 -->
            <div class="form-section" th:if="${contract.contractType.name() == '劳动合同'}">
                <h3>劳动合同内容</h3>
                <div class="form-row">
                    <div class="form-group">
                        <label for="workPosition">工作岗位</label>
                        <input type="text" id="workPosition" th:field="*{contract.workPosition}">
                    </div>
                    <div class="form-group">
                        <label for="workLocation">工作地点</label>
                        <input type="text" id="workLocation" th:field="*{contract.workLocation}">
                    </div>
                </div>
            </div>

            <div class="form-section" th:if="${contract.contractType.name() == '委托合同'}">
                <h3>委托合同内容</h3>
                <div class="form-row">
                    <div class="form-group full-width">
                        <label for="entrustContent">委托事项</label>
                        <textarea id="entrustContent" th:field="*{contract.entrustContent}" rows="3"></textarea>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group full-width">
                        <label for="entrustScope">委托范围</label>
                        <textarea id="entrustScope" th:field="*{contract.entrustScope}" rows="3"></textarea>
                    </div>
                </div>
            </div>

            <div class="form-section" th:if="${contract.contractType.name() == '服务协议'}">
                <h3>服务协议内容</h3>
                <div class="form-row">
                    <div class="form-group full-width">
                        <label for="serviceContent">服务内容</label>
                        <textarea id="serviceContent" th:field="*{contract.serviceContent}" rows="5"></textarea>
                    </div>
                </div>
            </div>

            <!-- 附件上传部分 -->
            <div class="form-section">
                <h3>合同附件</h3>
                <div class="attachment-upload">
                    <input type="file" id="fileInput" accept=".pdf,.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png">
                    <button type="button" class="btn btn-upload" onclick="uploadFile()">上传附件</button>
                </div>

                <div class="attachment-list">
                    <table>
                        <thead>
                        <tr>
                            <th>文件名</th>
                            <th>类型</th>
                            <th>上传时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr th:each="attachment : ${attachments}">
                            <td th:text="${attachment.fileName}"></td>
                            <td th:text="${attachment.fileType}"></td>
                            <td th:text="${#temporals.format(attachment.createTime, 'yyyy-MM-dd HH:mm')}"></td>
                            <td>
                                <a th:href="@{'/contract/download/' + ${attachment.attachmentId}}"
                                   class="btn-download">下载</a>
                                <button type="button" class="btn-delete"
                                        th:data-contract-id="${contract.contractId}"
                                        th:data-attachment-id="${attachment.attachmentId}"
                                        onclick="deleteAttachment(this)">删除</button>
                            </td>
                        </tr>
                        <tr th:if="${attachments.isEmpty()}">
                            <td colspan="4" class="no-attachments">暂无附件</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary">保存修改</button>
                <button type="button" class="btn btn-cancel" onclick="window.history.back()">取消</button>
            </div>
        </form>
    </main>
</div>
<script th:src="@{/js/contract-edit.js}"></script>
</body>
</html>